package com.aiwei.tdjk.controller;

import com.aiwei.tdjk.common.Constants;
import com.aiwei.tdjk.dto.request.SaleOrderHeaderPagerRequest;
import com.aiwei.tdjk.entity.SaleOrderHeader;
import com.aiwei.tdjk.entity.User;
import com.aiwei.tdjk.enums.SaleOrderStatusEnum;
import com.aiwei.tdjk.service.SaleOrderHeaderService;
import com.aiwei.tdjk.service.SaleOrderItemService;
import com.aiwei.tdjk.utils.UserUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/admin/saleOrderHeader")
public class SaleOrderHeaderController extends BaseController {

	@Autowired
	private SaleOrderHeaderService saleOrderHeaderService;

	@Autowired
	private SaleOrderItemService saleOrderItemService;

	protected void setCommonData(Model model) {
		model.addAttribute("statusList", SaleOrderStatusEnum.values());
	}

	@RequestMapping(value = {"/list"}, method = RequestMethod.GET)
	public String list(SaleOrderHeaderPagerRequest request, Model model) {

		Subject subject = SecurityUtils.getSubject();
		if (subject.hasRole("admin")){
		}else{
			User user = UserUtil.getUserFromSession();
			request.setSaleManName(user.getFullName());
		}
		request.setOrderType("丰源轮胎海外销售订单");
		setCommonData(model);
		model.addAttribute("page", saleOrderHeaderService.find(request));
		model.addAttribute("saleOrderHeader", request);
		return viewName("list");
	}

	@RequestMapping(value = {"/listInner"}, method = RequestMethod.GET)
	public String listInner(SaleOrderHeaderPagerRequest request, Model model) {
		request.setOrderType("丰源轮胎内销销售订单");
		setCommonData(model);
		model.addAttribute("page", saleOrderHeaderService.find(request));
		model.addAttribute("saleOrderHeader", request);
		return viewName("listInner");
	}


	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(Model model, @PathVariable("id") Long id) {

		SaleOrderHeader saleOrderHeader = saleOrderHeaderService.get(id);
		model.addAttribute("m", saleOrderHeader);
		model.addAttribute(Constants.OP_NAME, "查看");
		//获取订单详情
		model.addAttribute("saleOrderItems", saleOrderItemService.getListByOrderCode(saleOrderHeader.getSaleordercode()));
		return viewName("editForm");
	}




}
